<?php

/**
 * Libro form base class.
 *
 * @method Libro getObject() Returns the current form's model object
 *
 * @package    universidad
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseLibroForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'              => new sfWidgetFormInputHidden(),
      'titulo'          => new sfWidgetFormInputText(),
      'tipo'            => new sfWidgetFormChoice(array('choices' => array('Digital' => 'Digital', 'Impreso' => 'Impreso'))),
      'direcciones'     => new sfWidgetFormTextarea(),
      'titulo_original' => new sfWidgetFormInputText(),
      'traductor'       => new sfWidgetFormInputText(),
      'resumen'         => new sfWidgetFormTextarea(),
      'tomo'            => new sfWidgetFormInputText(),
      'edicion'         => new sfWidgetFormInputText(),
      'ano_edicion'     => new sfWidgetFormInputText(),
      'ano_publicacion' => new sfWidgetFormInputText(),
      'paginas'         => new sfWidgetFormInputText(),
      'estado_fisico'   => new sfWidgetFormChoice(array('choices' => array('Delicado' => 'Delicado', 'Bueno' => 'Bueno', 'Nuevo' => 'Nuevo'))),
      'condicion'       => new sfWidgetFormChoice(array('choices' => array('Donacion' => 'Donacion', 'Prestamo' => 'Prestamo'))),
      'estado'          => new sfWidgetFormChoice(array('choices' => array('Prestado' => 'Prestado', 'Disponible' => 'Disponible', 'Reparacion' => 'Reparacion'))),
      'genero'          => new sfWidgetFormInputText(),
      'creador_id'      => new sfWidgetFormInputText(),
      'autor_id'        => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Autor'), 'add_empty' => false)),
      'editorial_id'    => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Editorial'), 'add_empty' => false)),
      'lengua_id'       => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Lengua'), 'add_empty' => false)),
      'palabra_list'    => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Palabra')),
    ));

    $this->setValidators(array(
      'id'              => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'titulo'          => new sfValidatorString(array('max_length' => 255)),
      'tipo'            => new sfValidatorChoice(array('choices' => array(0 => 'Digital', 1 => 'Impreso'), 'required' => false)),
      'direcciones'     => new sfValidatorString(array('required' => false)),
      'titulo_original' => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'traductor'       => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'resumen'         => new sfValidatorString(array('required' => false)),
      'tomo'            => new sfValidatorInteger(array('required' => false)),
      'edicion'         => new sfValidatorInteger(array('required' => false)),
      'ano_edicion'     => new sfValidatorInteger(array('required' => false)),
      'ano_publicacion' => new sfValidatorInteger(array('required' => false)),
      'paginas'         => new sfValidatorInteger(array('required' => false)),
      'estado_fisico'   => new sfValidatorChoice(array('choices' => array(0 => 'Delicado', 1 => 'Bueno', 2 => 'Nuevo'), 'required' => false)),
      'condicion'       => new sfValidatorChoice(array('choices' => array(0 => 'Donacion', 1 => 'Prestamo'), 'required' => false)),
      'estado'          => new sfValidatorChoice(array('choices' => array(0 => 'Prestado', 1 => 'Disponible', 2 => 'Reparacion'), 'required' => false)),
      'genero'          => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'creador_id'      => new sfValidatorInteger(),
      'autor_id'        => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Autor'))),
      'editorial_id'    => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Editorial'))),
      'lengua_id'       => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Lengua'))),
      'palabra_list'    => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Palabra', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('libro[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Libro';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['palabra_list']))
    {
      $this->setDefault('palabra_list', $this->object->Palabra->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->savePalabraList($con);

    parent::doSave($con);
  }

  public function savePalabraList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['palabra_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Palabra->getPrimaryKeys();
    $values = $this->getValue('palabra_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Palabra', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Palabra', array_values($link));
    }
  }

}
